<?php
namespace Foam;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

use Foam\Model\CategorieTable;
use Foam\Model\ServiceTable;
use Foam\Model\AllergeneTable;
use Foam\Model\CarteTable;
use Foam\Model\CarteProduitTable;
use Foam\Model\ContenantTable;
use Foam\Model\DlcTable;
use Foam\Model\FamilleTable;
use Foam\Model\FicheTechniqueTable;
use Foam\Model\FournisseurTable;
use Foam\Model\GammeTable;
use Foam\Model\IngredientTable;
use Foam\Model\NoteTable;
use Foam\Model\ProductionTable;
use Foam\Model\ProduitTable;
use Foam\Model\ProduitAllergeneTable;
use Foam\Model\SousFamilleTable;
use Foam\Model\SousGammeTable;
use Foam\Model\TvaTable;

class Module
{ 
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
        
        $sharedEvents = $eventManager->getSharedManager();
        $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
            $controller = $e->getTarget();
            $route = $controller->getEvent()->getRouteMatch();
            $e->getViewModel()->setVariables(
                array('controllerName'=> $route->getParam('__CONTROLLER__', 'index'),
                    'actionName' => $route->getParam('action', 'index'),
                    'moduleName' => strtolower(__NAMESPACE__))
            );
        }, 100);
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
    
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
    
    public function getServiceConfig()
    {
        return array(
            'factories' => array(   
                // Service for Categorie table
                'Foam\Model\CategorieTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new CategorieTable($dbAdapter);
                    return $table;
                },                          
                // Service for Service table
                'Foam\Model\ServiceTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new ServiceTable($dbAdapter);
                    return $table;
                },                          
                // Service for Allergene table
                'Foam\Model\AllergeneTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new AllergeneTable($dbAdapter);
                    return $table;
                },
                // Service for Carte table
                'Foam\Model\CarteTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new CarteTable($dbAdapter);
                    return $table;
                },                
                // Service for CarteProduit table
                'Foam\Model\CarteProduitTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new CarteProduitTable($dbAdapter);
                    return $table;
                },  
                // Service for Contenant table
                'Foam\Model\ContenantTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new ContenantTable($dbAdapter);
                    return $table;
                },  
                // Service for Dlc table
                'Foam\Model\DlcTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new DlcTable($dbAdapter);
                    return $table;
                }, 
                // Service for Famille table
                'Foam\Model\FamilleTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new FamilleTable($dbAdapter);
                    return $table;
                }, 
                // Service for FicheTechnique table
                'Foam\Model\FicheTechniqueTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new FicheTechniqueTable($dbAdapter);
                    return $table;
                },                       
                // Service for Fournisseur table
                'Foam\Model\FournisseurTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new FournisseurTable($dbAdapter);
                    return $table;
                },                        
                // Service for Gamme table
                'Foam\Model\GammeTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new GammeTable($dbAdapter);
                    return $table;
                },                        
                // Service for Ingredient table
                'Foam\Model\IngredientTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new IngredientTable($dbAdapter);
                    return $table;
                },                        
                // Service for Note table
                'Foam\Model\NoteTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new NoteTable($dbAdapter);
                    return $table;
                },                  
                // Service for Production table
                'Foam\Model\ProductionTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new ProductionTable($dbAdapter);
                    return $table;
                },                        
                // Service for Produit table
                'Foam\Model\ProduitTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new ProduitTable($dbAdapter);
                    return $table;
                },                        
                // Service for ProduitAllergene table
                'Foam\Model\ProduitAllergeneTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new ProduitAllergeneTable($dbAdapter);
                    return $table;
                },                        
                // Service for SousFamille table
                'Foam\Model\SousFamilleTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new SousFamilleTable($dbAdapter);
                    return $table;
                },                        
                // Service for SousGamme table
                'Foam\Model\SousGammeTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new SousGammeTable($dbAdapter);
                    return $table;
                },                        
                // Service for Tva table
                'Foam\Model\TvaTable' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter2');
                    $table = new TvaTable($dbAdapter);
                    return $table;
                },
            ),
        );
    }
}
